/*
 *  The <interrupts.h> header file defines the prototypes of the Interrupt Service Routines (ISRs) 
 */

#ifndef _INTERRUPTS_H
#define _INTERRUPTS_H

#include <types.h>
#include <i386_arch.h>

/* Interrupt Vector Constants */

/* Interrupt Vectors reserved by processor */
#define DIVIDE_VECTOR           0x00    /* divide error (overflow, zero denominator) */
#define DEBUG_VECTOR            0x01    /* debug exception */
#define NMI_VECTOR              0x02    /* non-maskable hardware interrupt (NMI) */
#define BREAKPOINT_VECTOR       0x03    /* INT3 instruction (debugger breakpoint) */
#define OVERFLOW_VECTOR         0x04    /* INTO instruction detected overflow */
#define BOUND_VECTOR            0x05    /* BOUND instruction detected overrange */
#define OPCODE_VECTOR           0x06    /* invalid instruction opcode */
#define NOFPU_VECTOR            0x07    /* no coprocessor (ESC, WAIT instructions) */
#define DBLFAULT_VECTOR         0x08    /* double fault (when INT vector exceeds IDT limit) */
#define FPUOVERRUN_VECTOR       0x09    /* coprocessor segment overrun */
#define INVALIDTSS_VECTOR       0x0A    /* invalid task state segment (TSS) */
#define SEGMENT_VECTOR          0x0B    /* segment not present */
#define STACK_VECTOR            0x0C    /* stack fault */
#define GPF_VECTOR              0x0D    /* general protection fault (GPF) */
#define PAGE_VECTOR             0x0E    /* page fault  */
#define INT0x0F_VECTOR          0x0F    /* (reserved by Intel) */
#define FPU_VECTOR              0x10    /* coprocessor error (ESC, WAIT instructions) */
#define ALIGN_VECTOR            0x11    /* alignment check (486+ only) */
#define MACHINE_VECTOR          0x12    /* machine check (Pentium+ only) */
#define FPUPIII_VECTOR          0x13    /* FPU exception (Pentium III+) */
#define INT0x14_VECTOR          0x14    /* (reserved by Intel) */
#define INT0x15_VECTOR          0x15    /* (reserved by Intel) */
#define INT0x16_VECTOR          0x16    /* (reserved by Intel) */
#define INT0x17_VECTOR          0x17    /* (reserved by Intel) */
#define INT0x18_VECTOR          0x18    /* (reserved by Intel) */
#define INT0x19_VECTOR          0x19    /* (reserved by Intel) */
#define INT0x1A_VECTOR          0x1A    /* (reserved by Intel) */
#define INT0x1B_VECTOR          0x1B    /* (reserved by Intel) */
#define INT0x1C_VECTOR          0x1C    /* (reserved by Intel) */
#define INT0x1D_VECTOR          0x1D    /* (reserved by Intel) */
#define INT0x1E_VECTOR          0x1E    /* (reserved by Intel) */
#define INT0x1F_VECTOR          0x1F    /* (reserved by Intel) */

/* Interrupt Vectors used by Horizon */
#define SYSCALL_VECTOR           0x20   /* used for system calls */

#define INT0x21_VECTOR          0x21    /* (reserved) */
#define INT0x22_VECTOR          0x22    /* (reserved) */
#define INT0x23_VECTOR          0x23    /* (reserved) */
#define INT0x24_VECTOR          0x24    /* (reserved) */
#define INT0x25_VECTOR          0x25    /* (reserved) */
#define INT0x26_VECTOR          0x26    /* (reserved) */
#define INT0x27_VECTOR          0x27    /* (reserved) */
#define INT0x28_VECTOR          0x28    /* (reserved) */
#define INT0x29_VECTOR          0x29    /* (reserved) */
#define INT0x2A_VECTOR          0x2A    /* (reserved) */
#define INT0x2B_VECTOR          0x2B    /* (reserved) */
#define INT0x2C_VECTOR          0x2C    /* (reserved) */
#define INT0x2D_VECTOR          0x2D    /* (reserved) */
#define INT0x2E_VECTOR          0x2E    /* (reserved) */
#define INT0x2F_VECTOR          0x2F    /* (reserved) */


/* Interrupt Vectors assigned to IRQs */
/* Interrupt Vectors used by Master PIC (8259M) */
#define IRQ00_VECTOR            0x30    /* IRQ 00 - Timer */
#define IRQ01_VECTOR            0x31    /* IRQ 01 - Keyboard */
#define IRQ02_VECTOR            0x32    /* IRQ 02 - Cascade (for 2nd 8259) */
#define IRQ03_VECTOR            0x33    /* IRQ 03 - RS232 Port 2 */
#define IRQ04_VECTOR            0x34    /* IRQ 04 - RS232 Port 1*/
#define IRQ05_VECTOR            0x35    /* IRQ 05 - Parallel Port */
#define IRQ06_VECTOR            0x36    /* IRQ 06 - Floppy */
#define IRQ07_VECTOR            0x37    /* IRQ 07 - (N/D) */

/* Interrupt Vectors used by Slave PIC (8259S) */
#define IRQ08_VECTOR            0x38    /* IRQ 08 - Realtime Clock */
#define IRQ09_VECTOR            0x39    /* IRQ 09 - (N/D) */
#define IRQ10_VECTOR            0x3A    /* IRQ 10 - (N/D) */
#define IRQ11_VECTOR            0x3B    /* IRQ 11 - (N/D) */
#define IRQ12_VECTOR            0x3C    /* IRQ 12 - PS/2 Mouse */
#define IRQ13_VECTOR            0x3D    /* IRQ 13 - 386 Coprocessor */
#define IRQ14_VECTOR            0x3E    /* IRQ 14 - Primary IDE channel */
#define IRQ15_VECTOR            0x3F    /* IRQ 15 - Secondary IDE channel */


/*
 * ISRs - Interrupt Service Routines
 * See interrupts.s for descriptions
 */

// Intel defined Interrupts
extern void isr_divide(void);
extern void isr_debug(void);
extern void isr_nmi(void);
extern void isr_breakpoint(void);
extern void isr_overflow(void);
extern void isr_bound(void);
extern void isr_opcode(void);
extern void isr_nofpu(void);
extern void isr_doublefault(void);
extern void isr_fpuoverrun(void);
extern void isr_invalidtss(void);
extern void isr_nosegment(void);
extern void isr_stacksegment(void);
extern void isr_gpf(void);
extern void isr_pagefault(void);
extern void isr_mathfault(void);
extern void isr_alignment(void);
extern void isr_machine(void);
extern void isr_fpesimd(void);
extern void isr_reserved(void);

// IRQ ISRs
extern void isr_irq_00(void);
extern void isr_irq_01(void);
extern void isr_irq_02(void);
extern void isr_irq_03(void);
extern void isr_irq_04(void);
extern void isr_irq_05(void);
extern void isr_irq_06(void);
extern void isr_irq_07(void);
extern void isr_irq_08(void);
extern void isr_irq_09(void);
extern void isr_irq_10(void);
extern void isr_irq_11(void);
extern void isr_irq_12(void);
extern void isr_irq_13(void);
extern void isr_irq_14(void);
extern void isr_irq_15(void);

// Extra ISRs
extern void isr_unhandled(void);
extern void isr_syscall(void);

// Extra IRQs handling
extern void initialize_IRQ(void);
extern void handle_IRQ(uregs_t *uregs);
extern void assign_IRQ(int irq_num,dword_t isr);
extern void unassign_IRQ(int irq_num);

#endif /* _INTERRUPTS_H */

